<script setup lang="ts">
import { OrderType } from '@/enums'
import { onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useMedicine } from '@/composables/medicine'
import { confirmReceiveService } from '@/api/medicine'
import { Dialog, Toast } from 'vant'
const route = useRoute()
const router = useRouter()
const { orderDetail, getOrderDetail } = useMedicine(route.params.id as string)
const goToExpressDetail = () => {
  router.push(`/medicine/express/${orderDetail.value!.id}`)
}

// 确认收货
const confirmReceive = async () => {
  if (orderDetail.value!.status !== OrderType.MedicineTake) {
    return Toast('当前订单状态不能确认收货')
  }
  try {
    Dialog.confirm({
      title: '确认收货',
      message: '确认收到货了？',
    }).then(async () => {
      await confirmReceiveService(orderDetail.value!.id)
      getOrderDetail()
      Toast('确认收货成功')
    })
  } catch (error) {
    console.log(error)
  }
}

onMounted(() => {})
</script>

<template>
  <div class="order-detail-page" v-if="orderDetail">
    <cp-nav-bar title="药品订单详情" />
    <div class="order-head">
      <div class="card" @click="goToExpressDetail">
        <div class="logistics">
          <p>{{ orderDetail.expressInfo?.content || '已通知快递取件' }}</p>
          <p>{{ orderDetail.expressInfo?.time || '--' }}</p>
        </div>
        <van-icon name="arrow" />
      </div>
    </div>
    <!-- 药品列表 -->
    <div class="order-medical">
      <div class="head">
        <h3>优医药房</h3>
        <small>优医质保 假一赔十</small>
      </div>
      <div class="item van-hairline--top" v-for="i in orderDetail.medicines" :key="i.id">
        <img class="img" :src="i.avatar" alt="" />
        <div class="info">
          <p class="name">
            <span> {{ i.name }}</span>
            <span>x{{ i.quantity }}</span>
          </p>
          <p class="size">
            <van-tag>{{ i.prescriptionFlag === 1 ? '处方药' : '非处方药' }}</van-tag>
            <span>{{ i.specs }}</span>
          </p>
          <p class="price">￥{{ i.amount }}</p>
        </div>
        <div class="desc">{{ i.usageDosag }}</div>
      </div>
    </div>
    <div class="order-detail">
      <van-cell-group>
        <van-cell title="药品金额" :value="`￥${orderDetail.payment}`" />
        <van-cell title="运费" :value="`￥${orderDetail.expressFee}`" />
        <van-cell title="优惠券" :value="`-￥${orderDetail.couponDeduction}`" />
        <van-cell title="实付款" :value="`￥${orderDetail.actualPayment}`" class="price" />
        <van-cell title="订单编号" :value="orderDetail.orderNo" />
        <van-cell title="创建时间" :value="orderDetail.createTime" />
        <van-cell title="支付时间" :value="orderDetail.payTime" />
        <van-cell
          title="支付方式"
          :value="orderDetail.paymentMethod === 0 ? '微信支付' : '支付宝支付'"
        />
      </van-cell-group>
    </div>
    <van-action-bar>
      <van-action-bar-button
        :type="orderDetail.status === OrderType.MedicineComplete ? 'danger' : 'primary'"
        :text="orderDetail.status === OrderType.MedicineComplete ? '已签收' : '确认收货'"
        @click="confirmReceive"
        :disabled="orderDetail.status !== OrderType.MedicineTake"
      />
    </van-action-bar>
  </div>
  <div class="order-detail-page" v-else>
    <cp-nav-bar title="药品订单详情" />
    <div class="loading">
      <van-loading type="spinner" vertical color="#16c2a3">加载中...</van-loading>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-detail-page {
  padding-top: 46px;
  padding-bottom: 65px;
  .loading {
    position: absolute;
    top: 0;
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--cp-bg);
  }
}
.order-head {
  position: relative;
  padding: 15px;
  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 80px;
    background: linear-gradient(180deg, rgba(44, 181, 165, 0), rgba(44, 181, 165, 0.2));
    border-bottom-left-radius: 150px 20px;
    border-bottom-right-radius: 150px 20px;
  }
  .card {
    height: 74px;
    background-color: #fff;
    border-radius: 8px;
    position: relative;
    display: flex;
    align-items: center;
    padding: 0 15px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    .logistics {
      flex: 1;
      p {
        &:first-child {
          color: var(--cp-primary);
        }
        &:last-child {
          color: var(--cp-tag);
          font-size: 13px;
          margin-top: 5px;
        }
      }
    }
    .van-icon {
      color: var(--cp-tip);
    }
  }
}
:deep(.van-cell) {
  .van-cell__title {
    font-size: 16px;
  }
  .van-cell__value {
    font-size: 16px;
  }
  &.price {
    .van-cell__value {
      font-size: 18px;
      color: var(--cp-price);
    }
  }
}
.order-medical {
  background-color: #fff;
  padding: 0 15px;
  .head {
    display: flex;
    height: 54px;
    align-items: center;
    > h3 {
      font-size: 16px;
      font-weight: normal;
    }
    > small {
      font-size: 13px;
      color: var(--cp-tag);
      margin-left: 10px;
    }
  }
  .item {
    display: flex;
    flex-wrap: wrap;
    padding: 15px 0;
    .img {
      width: 80px;
      height: 70px;
      border-radius: 2px;
      overflow: hidden;
    }
    .info {
      padding-left: 15px;
      width: 250px;
      .name {
        display: flex;
        font-size: 15px;
        margin-bottom: 5px;
        > span:first-child {
          width: 200px;
        }
        > span:last-child {
          width: 50px;
          text-align: right;
        }
      }
      .size {
        margin-bottom: 5px;
        .van-tag {
          background-color: var(--cp-primary);
          vertical-align: middle;
        }
        span:not(.van-tag) {
          margin-left: 10px;
          color: var(--cp-tag);
          vertical-align: middle;
        }
      }
      .price {
        font-size: 16px;
        color: #eb5757;
      }
    }
    .desc {
      width: 100%;
      background-color: var(--cp-bg);
      border-radius: 4px;
      margin-top: 10px;
      padding: 4px 10px;
      color: var(--cp-tip);
    }
  }
}
.van-action-bar {
  padding: 0 10px;
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
  .price {
    padding: 0 10px;
    > span {
      font-size: 18px;
      color: var(--cp-price);
    }
  }
}
</style>
